package com.ruoyi.web.controller.fire;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.domain.ResponseEntityInfo;
import com.ruoyi.common.enums.RequestMapper;
import com.ruoyi.common.utils.RequestService;
import com.ruoyi.fire.domain.TUnit;
import com.ruoyi.fire.domain.dto.SupervisorUnitDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fire.domain.ERegion;
import com.ruoyi.fire.service.IERegionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import static org.apache.poi.sl.draw.binding.STRectAlignment.R;

/**
 * 行政区域Controller
 *
 * @author ruoyi
 * @date 2025-10-30
 */
@Api(value = "行政区域", tags = "行政区域")
@RestController
@RequiredArgsConstructor
@RequestMapping("/fire/region")
public class ERegionController extends BaseController {
    @Autowired
    private IERegionService eRegionService;
    private final RequestService requestService;

    @ApiOperation("省级行政区域")
    @GetMapping("/allList")
    public AjaxResult allList() {
        LambdaQueryWrapper<ERegion> wrapper = Wrappers.lambdaQuery(ERegion.class);
        wrapper.eq(ERegion::getType, 2).eq(ERegion::getId, 3195L);
        return success(eRegionService.list(wrapper));
    }

    @ApiOperation("市行政区域")
    @GetMapping("/childrenCityList/{regionId}")
    public AjaxResult childrenList(@PathVariable Long regionId) {
        LambdaQueryWrapper<ERegion> queryWrapper = Wrappers.lambdaQuery(ERegion.class);
        queryWrapper.eq(ERegion::getType, 3).eq(ERegion::getProvinceId, regionId);
        return success(eRegionService.list(queryWrapper));
    }

    @ApiOperation("区县行政区域")
    @GetMapping("/childrenDistrictList/{regionId}")
    public AjaxResult childrenDistrictList(@PathVariable Long regionId) {
        LambdaQueryWrapper<ERegion> queryWrapper = Wrappers.lambdaQuery(ERegion.class);
        queryWrapper.eq(ERegion::getType, 4).eq(ERegion::getCityId, regionId);
        return success(eRegionService.list(queryWrapper));
    }

    /**
     * 查询行政区域列表
     */
    @PreAuthorize("@ss.hasPermi('system:region:list')")
    @GetMapping("/list")
    public TableDataInfo list(ERegion eRegion) {
        startPage();
        List<ERegion> list = eRegionService.selectERegionList(eRegion);
        return getDataTable(list);
    }

    /**
     * 导出行政区域列表
     */
    @PreAuthorize("@ss.hasPermi('system:region:export')")
    @Log(title = "行政区域", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ERegion eRegion) {
        List<ERegion> list = eRegionService.selectERegionList(eRegion);
        ExcelUtil<ERegion> util = new ExcelUtil<ERegion>(ERegion.class);
        util.exportExcel(response, list, "行政区域数据");
    }

    /**
     * 获取行政区域详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:region:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(eRegionService.selectERegionById(id));
    }

    /**
     * 新增行政区域
     */
    @PreAuthorize("@ss.hasPermi('system:region:add')")
    @Log(title = "行政区域", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ERegion eRegion) {
        return toAjax(eRegionService.insertERegion(eRegion));
    }

    /**
     * 修改行政区域
     */
    @PreAuthorize("@ss.hasPermi('system:region:edit')")
    @Log(title = "行政区域", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ERegion eRegion) {
        return toAjax(eRegionService.updateERegion(eRegion));
    }

    /**
     * 删除行政区域
     */
    @PreAuthorize("@ss.hasPermi('system:region:remove')")
    @Log(title = "行政区域", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(eRegionService.deleteERegionByIds(ids));
    }


    /**
     * 获取上级主管单位
     */
    @ApiOperation("获取上级主管单位")
    @GetMapping("/getSupervisorUnit/{regionId}")
    public AjaxResult getSupervisorUnit(@PathVariable Long regionId) {
        Map<String, Object> params = new HashMap<>();
        params.put("region_id", regionId);
        ResponseEntityInfo responseEntityInfo = requestService.advancedRequest(RequestMapper.GET_SUPERVISOR_UNIT, params);
        List<SupervisorUnitDto> subRegions = JSON.parseArray(JSON.toJSONString(responseEntityInfo.getData()), SupervisorUnitDto.class);
        return success(subRegions);
    }
}
